package com.mission.schedule.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mission.schedule.R;
import com.mission.schedule.adapter.utils.CommonAdapter;
import com.mission.schedule.adapter.utils.ViewHolder;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.CalenderNoteBean;
import com.mission.schedule.bean.WBA_FindFunctionMy;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.entity.CLCategoryTable;
import com.mission.schedule.entity.CLRepeatTable;
import com.mission.schedule.entity.ScheduleTable;
import com.mission.schedule.swipexlistview.SwipeXListViewNoHead;
import com.mission.schedule.swipexlistview.SwipeXListViewNoHead.onRightViewWidthListener;
import com.mission.schedule.utils.SharedPrefUtil;
import com.mission.schedule.utils.StringUtils;
import com.mission.schedule.utils.UriUtils;
import com.mission.schedule.utils.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MyRepeatAdapter extends CommonAdapter<Map<String, String>> implements
        onRightViewWidthListener {

    private Context context;
    private Handler handler;
    private List<Map<String, String>> mList;
    private SwipeXListViewNoHead swipeXlistview;
    SharedPrefUtil sharedPrefUtil = null;
    String color = "0";
    String userid = "";

    public MyRepeatAdapter(Context context, List<Map<String, String>> lDatas,
                           int layoutItemID, Handler handler, SwipeXListViewNoHead swipeXlistview) {
        super(context, lDatas, layoutItemID);
        this.context = context;
        this.mList = lDatas;
        this.handler = handler;
        this.swipeXlistview = swipeXlistview;
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        userid = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.USERID, "");
        swipeXlistview.setonRightViewWidthListener(this);
    }

    private void setOnclick(int positions, Map<String, String> map, int what) {
        Message message = Message.obtain();
        message.arg1 = positions;
        message.obj = map;
        message.what = what;
        handler.sendMessage(message);
    }

    @Override
    public void onRightViewWidth(int position) {
        if (position != 1 && position != 2) {
            swipeXlistview.setRightViewWidth(context.getResources()
                    .getDimensionPixelSize(R.dimen.friends_item_80));
        } else {
            swipeXlistview.setRightViewWidth(context.getResources()
                    .getDimensionPixelSize(R.dimen.friends_item_0));
        }
    }

    @SuppressLint("NewApi")
    @Override
    public void getViewItem(ViewHolder holder, final Map<String, String> map, final int position) {

        RelativeLayout date_ll = holder.getView(R.id.date_ll);
        TextView date_tv = holder.getView(R.id.date_tv);
        TextView time_tv = holder.getView(R.id.time_tv);
        TextView content_tv = holder.getView(R.id.content_tv);
        TextView delete_tv = holder.getView(R.id.delete_tv);
        LinearLayout delete_ll = holder.getView(R.id.delete_ll);
        RelativeLayout content_ll = holder.getView(R.id.content_ll);
        TextView riqi_tv = holder.getView(R.id.riqi_tv);
        TextView comename_tv = holder.getView(R.id.comename_tv);
        RelativeLayout context_rl = holder.getView(R.id.context_rl);
        TextView pasue_tv = holder.getView(R.id.pasue_tv);
        LinearLayout ll = holder.getView(R.id.ll);
        TextView nongli_tv = holder.getView(R.id.nongli_tv);
//        LinearLayout newcome_ll = holder.getView(R.id.newcome_ll);
//        TextView newcome_tv = holder.getView(R.id.newcome_tv);
        TextView personstate_tv = holder.getView(R.id.personstate_tv);
        ImageView yaundian = holder.getView(R.id.yaundian);
        TextView xcx_tv = holder.getView(R.id.xcx_tv);
        TextView repeat_bg_color = holder.getView(R.id.repeat_bg_color);
        ImageView important_icon = holder.getView(R.id.important_icon);

        delete_tv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                setOnclick(position, map, 0);
            }
        });
        content_ll.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                setOnclick(position, map, 1);
            }
        });
        if (position == 0) {
            date_ll.setVisibility(View.VISIBLE);
        } else if (map.get(CLRepeatTable.repType).equals(mList.get(position - 1).get(CLRepeatTable.repType))) {
            date_ll.setVisibility(View.GONE);
        } else if (map.get(CLRepeatTable.repType).equals("4") && mList.get(position - 1).get(CLRepeatTable.repType).equals("6")) {
            date_ll.setVisibility(View.GONE);
        } else if (map.get(CLRepeatTable.repType).equals("6") && mList.get(position - 1).get(CLRepeatTable.repType).equals("4")) {
            date_ll.setVisibility(View.GONE);
        } else {
            date_ll.setVisibility(View.VISIBLE);
        }

        repeat_bg_color.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Message message = Message.obtain();
                if ("2".equals(map.get(CLRepeatTable.repType))) {
                    message.arg1 = 2;
                } else if ("3".equals(map.get(CLRepeatTable.repType))) {
                    message.arg1 = 3;
                } else if ("4".equals(map.get(CLRepeatTable.repType))) {
                    message.arg1 = 4;
                } else if ("6".equals(map.get(CLRepeatTable.repType))) {
                    message.arg1 = 6;
                }
                message.obj = map;
                message.what = 2;
                handler.sendMessage(message);
            }
        });
        content_ll.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return false;
            }
        });
        String clockTime = "";
        String colorState = "";
        String alarmClockTime = map.get(CLRepeatTable.repTime);
        int beforTime = Integer.parseInt(map.get(CLRepeatTable.repBeforeTime));
        String beforeStr = "";

        if (beforTime == 0) {
            beforeStr = "0";
        } else if (beforTime == 5) {
            beforeStr = "-5";
        } else if (beforTime == 15) {
            beforeStr = "-15";
        } else if (beforTime == 30) {
            beforeStr = "-30";
        } else if (beforTime == 60) {
            beforeStr = "-1h";
        } else if (beforTime == 120) {
            beforeStr = "-2h";
        } else if (beforTime == 1440) {
            beforeStr = "-1d";
        } else if (beforTime == 2 * 1440) {
            beforeStr = "-2d";
        } else if (beforTime == 7 * 1440) {
            beforeStr = "-1w";
        } else {
            beforeStr = "0";
        }
        if (beforeStr.equals("0")) {
            clockTime = "  <font color='" + colorState + "' size='5px'>"
                    + alarmClockTime + "</font>";
        } else {
            clockTime = "  <font color='" + colorState + "' size='5px'>"
                    + alarmClockTime + "(" + beforeStr + ")" + "</font>";
        }
        String allday = "<font color='" + colorState + "' size='5px'>"
                + "全天" + "</font>";
        String strdate = map.get(CLRepeatTable.repTypeParameter)
                .replace("[", "").replace("]", "").replace("\n\"", "").replace("\n", "")
                .replace("\"", "").toString();
        String weekStr;

        if ("0".equals(map.get(CLRepeatTable.repColorType))) {
            personstate_tv.setVisibility(View.GONE);
        } else {
            if (!"".equals(map.get(CLRepeatTable.repColorType))) {
                String tagname = App.getDBcApplication().QueryTagNameData(
                        Integer.parseInt(map.get(CLRepeatTable.repColorType)))
                        .get(CLCategoryTable.ctgText);
                if ("未分类".equals(tagname)) {
                    personstate_tv.setVisibility(View.GONE);
                } else {
                    personstate_tv.setVisibility(View.VISIBLE);
                }
                personstate_tv.setText(tagname);
            }
        }


        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Utils.dipTopx(context, 80), Utils.dipTopx(context, 65));
        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        if (date_ll.getVisibility() == View.VISIBLE) {
            params.setMargins(0, Utils.dipTopx(context, 55), 0, 0);
            delete_ll.setLayoutParams(params);
            if (position == (mList.size() - 1)) {
                params1.setMargins(Utils.dipTopx(context, 8), Utils.dipTopx(context, 20), Utils.dipTopx(context, 8), Utils.dipTopx(context, 60));
            } else {
                params1.setMargins(Utils.dipTopx(context, 8), Utils.dipTopx(context, 20), Utils.dipTopx(context, 8), 0);
            }
            ll.setLayoutParams(params1);
        } else {
            params.setMargins(0, Utils.dipTopx(context, 13), 0, 0);
            delete_ll.setLayoutParams(params);
            if (position == (mList.size() - 1)) {
                params1.setMargins(Utils.dipTopx(context, 8), 0, Utils.dipTopx(context, 8), Utils.dipTopx(context, 60));
            } else {
                params1.setMargins(Utils.dipTopx(context, 8), 0, Utils.dipTopx(context, 8), 0);
            }
            ll.setLayoutParams(params1);
        }
        String nums = App.getDBcApplication().getCalenderNoteData(map.get(CLRepeatTable.repSourceDesc));
        if (!nums.equals("0") && !nums.isEmpty()) {
            nums = "(" + nums + ")";
        } else {
            nums = "";
        }
        if ("1".equals(map.get(CLRepeatTable.dataType))) {
            comename_tv.setText("@"+map.get(CLRepeatTable.repcommendedUserName));
            comename_tv.setVisibility(View.VISIBLE);
            String co = ((map.get(CLRepeatTable.repContent).length() > 2 &&
                    map.get(CLRepeatTable.repContent).substring(0,2).equals("#%")) ?
                    map.get(CLRepeatTable.repContent).substring(2, map.get(CLRepeatTable.repContent).length()) :
                    map.get(CLRepeatTable.repContent)) + " " +nums;
            SpannableStringBuilder style = new SpannableStringBuilder(co);
            style.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.gongkai_txt)),
                    ((map.get(CLRepeatTable.repContent).length() > 2 &&
                            map.get(CLRepeatTable.repContent).substring(0,2).equals("#%")) ?
                            map.get(CLRepeatTable.repContent).substring(2, map.get(CLRepeatTable.repContent).length()) :
                            map.get(CLRepeatTable.repContent)).length(), co.length(),
                    Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置指定位置文字的颜色
            content_tv.setText(style);
        } else {
            content_tv.setText((map.get(CLRepeatTable.repContent).length() > 2 &&
                    map.get(CLRepeatTable.repContent).substring(0,2).equals("#%")) ?
                    map.get(CLRepeatTable.repContent).substring(2, map.get(CLRepeatTable.repContent).length()) :
                    map.get(CLRepeatTable.repContent));
            comename_tv.setText("");
            comename_tv.setVisibility(View.GONE);
        }

        String displaytime = map.get(CLRepeatTable.repDisplayTime);
        if ("1".equals(displaytime)) {
            if ("1".equals(map.get(CLRepeatTable.repType))) {
                riqi_tv.setVisibility(View.GONE);
                nongli_tv.setVisibility(View.GONE);
                date_tv.setText("每天");
                time_tv.setText(Html.fromHtml(clockTime));
                repeat_bg_color.setVisibility(View.GONE);
                color = "0";
            } else if ("2".equals(map.get(CLRepeatTable.repType))) {
                repeat_bg_color.setVisibility(View.VISIBLE);
                riqi_tv.setVisibility(View.VISIBLE);
                nongli_tv.setVisibility(View.GONE);
                date_tv.setText("每周");
                if (strdate.equals("1")) {
                    weekStr = "一";
                } else if (strdate.equals("2")) {
                    weekStr = "二";
                } else if (strdate.equals("3")) {
                    weekStr = "三";
                } else if (strdate.equals("4")) {
                    weekStr = "四";
                } else if (strdate.equals("5")) {
                    weekStr = "五";
                } else if (strdate.equals("6")) {
                    weekStr = "六";
                } else {
                    weekStr = "日";
                }
                riqi_tv.setText("周" + weekStr);
                time_tv.setText(Html.fromHtml(clockTime));
                color = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.WEEKCOLOR1, "4");
            } else if ("3".equals(map.get(CLRepeatTable.repType))) {
                riqi_tv.setVisibility(View.VISIBLE);
                nongli_tv.setVisibility(View.GONE);
                repeat_bg_color.setVisibility(View.VISIBLE);
                date_tv.setText("每月");
                riqi_tv.setText(strdate + "日");
                time_tv.setText(Html.fromHtml(clockTime));
                color = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.MONTHCOLOR2, "3");
            } else if ("4".equals(map.get(CLRepeatTable.repType))) {
                riqi_tv.setVisibility(View.VISIBLE);
                date_tv.setText("每年");
                repeat_bg_color.setVisibility(View.VISIBLE);
                nongli_tv.setVisibility(View.GONE);
                riqi_tv.setText(strdate);
                time_tv.setText(Html.fromHtml(clockTime));
                color = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.YEARCOLOR3, "2");
            } else if ("6".equals(map.get(CLRepeatTable.repType))) {
                riqi_tv.setVisibility(View.VISIBLE);
                date_tv.setText("每年");
                repeat_bg_color.setVisibility(View.VISIBLE);
                nongli_tv.setVisibility(View.VISIBLE);
                riqi_tv.setText(strdate);
                time_tv.setText(Html.fromHtml(clockTime));
                color = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.YEARCOLOR3, "2");
            } else {
                repeat_bg_color.setVisibility(View.GONE);
                riqi_tv.setVisibility(View.GONE);
                nongli_tv.setVisibility(View.GONE);
                date_tv.setText("工作日");
                time_tv.setText(Html.fromHtml(clockTime));
                color = "0";
            }
        } else {
            if ("1".equals(map.get(CLRepeatTable.repType))) {
                riqi_tv.setVisibility(View.GONE);
                nongli_tv.setVisibility(View.GONE);
                repeat_bg_color.setVisibility(View.GONE);
                date_tv.setText("每天");
                time_tv.setText(Html.fromHtml(allday));
            } else if ("2".equals(map.get(CLRepeatTable.repType))) {
                riqi_tv.setVisibility(View.VISIBLE);
                repeat_bg_color.setVisibility(View.VISIBLE);
                nongli_tv.setVisibility(View.GONE);
                date_tv.setText("每周");
                if (strdate.equals("1")) {
                    weekStr = "一";
                } else if (strdate.equals("2")) {
                    weekStr = "二";
                } else if (strdate.equals("3")) {
                    weekStr = "三";
                } else if (strdate.equals("4")) {
                    weekStr = "四";
                } else if (strdate.equals("5")) {
                    weekStr = "五";
                } else if (strdate.equals("6")) {
                    weekStr = "六";
                } else {
                    weekStr = "日";
                }
                riqi_tv.setText("周" + weekStr);
                color = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.WEEKCOLOR1, "4");
                time_tv.setText(Html.fromHtml(allday));
            } else if ("3".equals(map.get(CLRepeatTable.repType))) {
                nongli_tv.setVisibility(View.GONE);
                date_tv.setText("每月");
                repeat_bg_color.setVisibility(View.VISIBLE);
                riqi_tv.setText(strdate + "日");
                time_tv.setText(Html.fromHtml(allday));
                color = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.MONTHCOLOR2, "3");
            } else if ("4".equals(map.get(CLRepeatTable.repType))) {
                nongli_tv.setVisibility(View.GONE);
                date_tv.setText("每年");
                repeat_bg_color.setVisibility(View.VISIBLE);
                riqi_tv.setText(strdate);
                riqi_tv.setVisibility(View.VISIBLE);
                color = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.YEARCOLOR3, "2");
                time_tv.setText(Html.fromHtml(allday));
            } else if ("6".equals(map.get(CLRepeatTable.repType))) {
                date_tv.setText("每年");
                nongli_tv.setVisibility(View.VISIBLE);
                riqi_tv.setVisibility(View.VISIBLE);
                riqi_tv.setText(strdate);
                repeat_bg_color.setVisibility(View.VISIBLE);
                color = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.YEARCOLOR3, "2");
                time_tv.setText(Html.fromHtml(allday));
            } else {
                nongli_tv.setVisibility(View.GONE);
                riqi_tv.setVisibility(View.GONE);
                date_tv.setText("工作日");
                repeat_bg_color.setVisibility(View.GONE);
                time_tv.setText(Html.fromHtml(allday));
            }
        }

        important_icon.setVisibility(View.GONE);
        if ("1".equals(map.get(CLRepeatTable.repIsImportant))) {
            if ("1".equals(map.get(CLRepeatTable.repIsPuase))) {
                pasue_tv.setVisibility(View.VISIBLE);
                pasue_tv.setText("暂停");
                context_rl.setBackground(context.getResources().getDrawable(R.drawable.bg_rep_pause));
                important_icon.setBackground(context.getResources().getDrawable(R.mipmap.icon_zhongyao1));
                important_icon.setVisibility(View.VISIBLE);
            } else {
                pasue_tv.setVisibility(View.GONE);
                context_rl.setBackground(context.getResources().getDrawable(R.drawable.bg_repeat_normal));
                important_icon.setBackground(context.getResources().getDrawable(R.mipmap.icon_zhongyao));
                important_icon.setVisibility(View.VISIBLE);
            }
        } else if ("1".equals(map.get(CLRepeatTable.repIsPuase))) {
            pasue_tv.setVisibility(View.VISIBLE);
            pasue_tv.setText("暂停");
            context_rl.setBackground(context.getResources().getDrawable(R.drawable.bg_rep_pause));
        } else {
            pasue_tv.setVisibility(View.GONE);
            context_rl.setBackground(context.getResources().getDrawable(R.drawable.bg_repeat_normal));
        }
        setBGcolor(repeat_bg_color, color);
        if ("1".equals(map.get(CLRepeatTable.repRead))) {
            yaundian.setVisibility(View.VISIBLE);
        } else {
            yaundian.setVisibility(View.GONE);
        }
        xcx_tv.setVisibility(View.GONE);
        WBA_FindFunctionMy.ListBean bean = App.getDBcApplication().get_tb_function_my(map.get(CLRepeatTable.remark2));
        if (bean != null && !bean.url.isEmpty()) {
            xcx_tv.setVisibility(View.VISIBLE);
        } else if (map.get(CLRepeatTable.repContent).length() > 2 && map.get(CLRepeatTable.repContent).substring(0,2).equals("#%")) {
            List<CalenderNoteBean.PageBean.ItemsBean> list =
                    App.getDBcApplication().getCalenderNoteDetailData(userid, map.get(CLRepeatTable.repSourceDesc), false);
            if (list.size() >= 1 && UriUtils.isUrl(list.get(0).contents)) {
                xcx_tv.setVisibility(View.VISIBLE);
            }
        } else {
            xcx_tv.setVisibility(View.GONE);
        }
    }

    void setBGcolor(TextView textView, String type) {
        switch (type) {
            case "1":
                textView.setBackground(context.getResources().getDrawable(R.drawable.repeat_bg_color1));
                break;
            case "2":
                textView.setBackground(context.getResources().getDrawable(R.drawable.repeat_bg_color2));
                break;
            case "3":
                textView.setBackground(context.getResources().getDrawable(R.drawable.repeat_bg_color3));
                break;
            case "4":
                textView.setBackground(context.getResources().getDrawable(R.drawable.repeat_bg_color4));
                break;
            case "0":
                textView.setBackground(context.getResources().getDrawable(R.drawable.repeat_bg_color1));
                break;
        }
    }
}
